/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.PlayerDao;
import cz.muni.fi.diplomka.datalayer.entity.PlayerEntity;
import cz.fi.muni.diplomka.common.enumeration.ColorType;
import cz.fi.muni.diplomka.common.enumeration.TitleType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class PlayerDaoImpl implements PlayerDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public PlayerDaoImpl() {
    }

    @Override
    public void addPlayer(PlayerEntity playerEntity) {
        if (playerEntity == null) {
            throw new IllegalArgumentException("PlayerEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO player (nickname, hashed_password, color, title) VALUES (?,?,?,?)";
            ps = con.prepareStatement(insert);

            ps.setString(1, playerEntity.getNickName());
            ps.setString(2, playerEntity.getHashedPassword());
            ps.setString(3, playerEntity.getColor().toString());
            ps.setString(4, playerEntity.getTitle().toString());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(PlayerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editPlayer(PlayerEntity playerEntity) {
        if (playerEntity == null) {
            throw new IllegalArgumentException("PlayerEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE player SET nickname=?, hashed_password=?, color=?, title=? WHERE id=?";

            ps = con.prepareStatement(update);

            ps.setString(1, playerEntity.getNickName());
            ps.setString(2, playerEntity.getHashedPassword());
            ps.setString(3, playerEntity.getColor().toString());
            ps.setString(4, playerEntity.getTitle().toString());
            ps.setLong(5, playerEntity.getId());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(PlayerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public PlayerEntity findPlayerByNickName(String login) {
        if (login == null) {
            throw new IllegalArgumentException("Login cant be null");
        }
        PlayerEntity playerEntity = new PlayerEntity();

        try {
            con = DbConnector.getDBConnection();
            String getPlayerByLogin = "SELECT * FROM player WHERE nickname=?";

            ps = con.prepareStatement(getPlayerByLogin);
            ps.setString(1, login);

            rs = ps.executeQuery();

            while (rs.next()) {
                playerEntity.setId(rs.getLong("id"));
                playerEntity.setNickName(rs.getString("nickname"));
                playerEntity.setHashedPassword(rs.getString("hashed_password"));
                playerEntity.setColor(ColorType.valueOf(rs.getString("color")));
                playerEntity.setTitle(TitleType.valueOf(rs.getString("title")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PlayerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return playerEntity;
    }

    @Override
    public PlayerEntity findPlayerById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        PlayerEntity playerEntity = new PlayerEntity();

        try {
            con = DbConnector.getDBConnection();
            String getPlayerOfPlayer = "SELECT * FROM player WHERE id=?";

            ps = con.prepareStatement(getPlayerOfPlayer);
            ps.setLong(1, id);

            rs = ps.executeQuery();

            while (rs.next()) {
                playerEntity.setId(rs.getLong("id"));
                playerEntity.setNickName(rs.getString("nickname"));
                playerEntity.setHashedPassword(rs.getString("hashed_password"));
                playerEntity.setColor(ColorType.valueOf(rs.getString("color")));
                playerEntity.setTitle(TitleType.valueOf(rs.getString("title")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PlayerDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return playerEntity;
    }
}
